<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleAdd" icon="el-icon-plus">新增字段</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="data" height="560" row-key="metadataId">
      <el-table-column label="字段名称" align="center" prop="metadataName" />
      <el-table-column label="是否必填" align="center" prop="isRequired">
        <template v-slot="scope">
          <p v-if="scope.row.isRequired == 1">是</p>
          <p v-if="scope.row.isRequired == 2">否</p>
        </template>
      </el-table-column>
      <el-table-column label="是否可见" align="center" prop="quesUserName">
        <template v-slot="scope">
          <p v-if="scope.row.isList == 1">是</p>
          <p v-if="scope.row.isList == 2">否</p>
        </template>
      </el-table-column>
      <el-table-column label="调整位置" align="center" prop="quesDescribe">
        <template v-slot="scope">
          <div v-if="scope.row.isOperation == 1">
            <el-link type="primary" @click="handleSort(scope.row, 1)">上移</el-link>
            <el-divider direction="vertical"/>
            <el-link type="warning" @click="handleSort(scope.row, 2)">下移</el-link>
          </div>
          <div v-else>
            <p>无操作权限</p>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="元数据操作" align="center" prop="action">
        <template v-slot="scope">
          <div v-if="scope.row.isOperation == 1">
            <el-link type="primary" @click="$refs['addForm'].edit(scope.row)">编辑</el-link>
            <el-divider direction="vertical"/>
            <el-link type="warning" @click="handleEdit(scope.row, 2)" v-if="scope.row.isList == 1">隐藏</el-link>
            <el-link type="warning" @click="handleEdit(scope.row, 1)" v-else>显示</el-link>
            <el-divider direction="vertical"/>
            <el-link type="danger" @click="handleDelete(scope.row)">删除</el-link>
          </div>
          <div v-else>
            <p>无操作权限</p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <AddAndUpdateForm @refresh="getList" ref="addForm"/>
  </div>
</template>

<script>
import AddAndUpdateForm from './components/AddAndUpdateForm'
import {
  metaList,
  delMeta,
  editMetadataSort,
  editMetadata
} from '../service'
export default {
  name: 'AccountManage',
  components: {
    AddAndUpdateForm
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      data: [],
      // 弹出层标题
      title: '',
    };
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    handleEdit(row, isList) {
      this.loading = true
      editMetadata({
        ...row,
        isList
      }).then(res => {
        if (res.code !== 200) return this.$message.error(res.message || '操作失败');
        this.getList();
        this.$message.success('操作成功~');
      })
        .finally(r => {
          this.loading = false
        })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      metaList().then((response) => {
        this.data = response.data
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs['addForm']['open'] = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const quesIds = row.metadataId;
      this.$confirm('是否确认删除"' + row.metadataName + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return delMeta(quesIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        });
    },
    /** 移动排序 */
    handleSort(row, type) {
      this.loading = true
      editMetadataSort({
        metadataId: row.metadataId,
        type
      })
        .then(res => {
          if (res.code !== 200) return this.$message.error(res.message || '操作失败');
          this.getList();
          this.$message.success('操作成功~');
        })
        .finally(r => {
          this.loading = false
        })
    }
  },
};
</script>
